<template>
  <el-dialog :title="Istitle" :visible="isShow" :before-close="handleClose">
    <el-form :model="addForm" :rules="rules" ref="addForm">
      <el-form-item label="角色名称" label-width="120px" prop="name">
        <el-input
          style="width: 500px"
          v-model="addForm.name"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="角色描述" label-width="120px">
        <el-input style="width: 500px" v-model="addForm.description">
        </el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" style="display: flex; justify-content: center">
      <el-button type="primary" @click="handleClose">取消</el-button>
      <el-button @click="add">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addRole, getRoleDetail, updateRole } from '@/api/setting'
// import { title } from '@/settings'
export default {
  data() {
    return {
      addForm: { name: '', description: '' },
      rules: {
        name: { required: true, message: '请输入活动名称', trigger: 'blur' }
      }
    }
  },

  props: {
    isShow: {
      type: Boolean,
      default: false
    },
    editId: {
      type: String,
      default: null
    }
  },
  computed: {
    Istitle() {
      return this.editId ? '修改' : '添加'
    }
  },

  methods: {
    handleClose() {
      this.$emit('update:isShow', false)
      this.addForm = {}
      this.$refs.addForm.resetFields()
    },
    async getRoleDetail(id) {
      const res = await getRoleDetail(id)

      this.addForm = res

      this.addForm.name = res.name
      this.addForm.description = res.description
    },
    add() {
      try {
        this.$refs.addForm.validate(async (valid) => {
          if (valid) {
            if (this.editId) {
              // 更新用户
              await updateRole(this.addForm)
              this.$parent.getRoleList()
              this.addForm = {}
            } else {
              // 新增

              await addRole(this.addForm)
            }
            this.$emit('update:isShow', false)
            this.$parent.getRoleList()
          }
        })
      } catch (error) {
        alert('请输入用户名')
      }
    }
  }
}
</script>

<style>
</style>